﻿// Setting.cpp: 实现文件
//

#include "pch.h"
#include "Snake.h"
#include "afxdialogex.h"
#include "Setting.h"
#include "resource.h"

#define EASY 1
#define MEDIUM 2
#define HARD 3
// Setting 对话框

IMPLEMENT_DYNAMIC(Setting, CDialogEx)

Setting::Setting(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_SETTING_DIALOG, pParent)
{

}

Setting::~Setting()
{
}

void Setting::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(Setting, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_OK, &Setting::OnBnClickedButtonOk)
	ON_WM_CLOSE()
END_MESSAGE_MAP()


// Setting 消息处理程序


BOOL Setting::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	int difficuty = GetPrivateProfileInt(_T("SETTING"), _T("DIFFICUTY"), 1, _T("./Setting.ini"));
	CButton* butten = NULL;
	switch (difficuty) {
	case EASY:
		butten = (CButton*)GetDlgItem(IDC_RADIO_EASY);
		break;
	case MEDIUM:
		butten = (CButton*)GetDlgItem(IDC_RADIO_MEDIUM);
		break;
	case HARD:
		butten = (CButton*)GetDlgItem(IDC_RADIO_HARD);
		break;
	}
	if (butten) {
		butten->SetCheck(TRUE);
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}


void Setting::OnBnClickedButtonOk()
{
	// TODO: 在此添加控件通知处理程序代码
	setDifficuty();
	CDialogEx::OnOK();
}


void Setting::OnClose()
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	setDifficuty();
	CDialogEx::OnOK();
}


void Setting::setDifficuty()
{
	// TODO: 在此处添加实现代码.
	int id = GetCheckedRadioButton(IDC_RADIO_EASY, IDC_RADIO_HARD);
	CString difficuty;
	switch (id)
	{
	case IDC_RADIO_EASY:
		difficuty.Format(_T("%d"), EASY);
		break;
	case IDC_RADIO_MEDIUM:
		difficuty.Format(_T("%d"), MEDIUM);
		break;
	case IDC_RADIO_HARD:
		difficuty.Format(_T("%d"), HARD);
		break;
	}
	WritePrivateProfileString(_T("SETTING"), _T("DIFFICUTY"), difficuty, _T("./Setting.ini"));
}
